package cc.bable.alipay.starter.utils;

import cc.bable.alipay.starter.properties.AlipayProperties;
import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.stream.Collectors;

public class NotifyUtils {

	private static final Logger LOGGER = LoggerFactory.getLogger(NotifyUtils.class);

	private NotifyUtils() {
	}

	public static boolean rsaCheck(AlipayProperties properties, Map<String, String[]> requestParams) {
		try {
			return rsaCheck(properties, requestParams, false);
		} catch (Exception var3) {
			LOGGER.warn(var3.getMessage(), var3);
			return false;
		}
	}

	public static boolean rsaCheck(AlipayProperties properties, Map<String, String[]> requestParams, boolean throwException) throws AlipayApiException {
		if (requestParams == null) {
			if (throwException) {
				throw new NullPointerException("requestParams cann't be null");
			} else {
				return false;
			}
		} else {
			Map params = (Map)requestParams.entrySet().stream().filter(NotifyUtils::entryCheck).collect(Collectors.toMap((e) -> {
				return (String)e.getKey();
			}, (e) -> {
				return String.join(",", (CharSequence[])e.getValue());
			}));

			try {
				return AlipaySignature.rsaCheckV1(params, properties.getAlipayPublicKey(), "UTF-8", "RSA2");
			} catch (AlipayApiException var5) {
				LOGGER.warn(var5.getMessage(), var5);
				if (throwException) {
					throw var5;
				} else {
					return false;
				}
			}
		}
	}

	private static boolean entryCheck(Map.Entry<String, String[]> entry) {
		return entry != null && entry.getKey() != null && entry.getValue() != null;
	}
}
